<template>
  <div class="main">
    <div class="images_box">
      <div class="form_box">
        <div class="avatar_box">
          <img src="../../assets/images/login/photo.png" alt />
        </div>
        <h1>用户登录</h1>
        <el-form
          class="login_form"
          label-width="100px"
          :rules="loginRule"
          :model="userForm"
          ref="loginRef"
        >
          <el-form-item label="用户名:" prop="userName">
            <el-input
              v-model="userForm.userName"
              prefix-icon="iconfont icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码:" prop="passWord">
            <el-input
              v-model="userForm.passWord"
              prefix-icon="iconfont icon-lock_fill"
              type="password"
              :show-password="true"
            ></el-input>
          </el-form-item>
        </el-form>
        <div>
          <el-button type="primary" @click="login()">登录</el-button>
          <el-button type="primary" @click="dialogVisible = true"
            >注册</el-button
          >
        </div>
      </div>
    </div>
    <el-dialog
      title="注册"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <div>
        <el-form
          class="register_form login_form"
          label-width="100px"
          :rules="loginRule"
          :model="userForm"
          ref="registerRef"
        >
          <el-form-item label="用户名:" prop="userName">
            <el-input
              v-model="userForm.userName"
              @change="checkUserName()"
            ></el-input>
            <div class="span">{{ this.userNameSpan }}</div>
          </el-form-item>
          <el-form-item label="密码:" prop="passWord">
            <el-input
              v-model="userForm.passWord"
              type="password"
              :show-password="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="userForm.phone" @blur="checkPhone()"></el-input>
            <div class="span">{{ this.phoneSpan }}</div>
          </el-form-item>
          <el-form-item label="真实姓名" prop="realName">
            <el-input v-model="userForm.realName"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="register()" :disabled="registerCommit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { login, checkInfo, register } from '../../assets/js/api/user.js';
import { validatePhone } from '../../assets/js/common/commonUtil.js';
import { Link } from '../../assets/js/style/Line.js';
export default {
  data () {
    return {
      userForm: {
        userName: '',
        passWord: '',
        phone: '',
        realName: ''
      },
      dialogVisible: false,
      registerCommit: true,
      userNameCheckNum: 0,
      checkPhoneNum: 0,
      userNameSpan: '',
      phoneSpan: '',
      loginRule: {
        userName: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '用户名长度在3-10位之间', trigger: 'blur' }
        ],
        passWord: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 5, max: 12, message: '密码长度在5-12位之间', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        realName: [
          { required: true, message: '请输入真实姓名', trigger: 'blur' }
        ]
      }
    };
  },
  created: {
    Link
  },
  methods: {
    login () {
      this.$refs.loginRef.validate(async valid => {
        if (!valid) {
          return false;
        }
        await login(this.userForm).then(res => {
          if (res.code !== 1) {
            return this.$message.error(res.msg);
          }
          this.$message.success(res.msg);
          this.$router.push('/home');
        });
      });
    },
    async checkUserName () {
      if (this.userForm.userName === '') {
        this.userNameSpan = '';
        this.userNameCheckNum = 0;
        this.registerCommit = true;
        return;
      }
      await checkInfo({ userName: this.userForm.userName }).then(res => {
        if (res.code !== 1) {
          if (this.userNameCheckNum !== 0) {
            this.userNameCheckNum = 0;
          }
          this.userNameSpan = '用户名已存在';
          this.registerCommit = true;
        } else {
          if (this.userNameCheckNum !== 1) {
            this.userNameCheckNum = 1;
          }
          this.userNameSpan = '';
          if (this.userNameCheckNum === 1 && this.checkPhoneNum === 1) {
            this.registerCommit = false;
          }
        }
      });
    },
    async checkPhone () {
      if (this.userForm.phone === '') {
        this.phoneSpan = '';
        this.checkPhoneNum = 0;
        this.registerCommit = true;
        return;
      }
      const result = validatePhone(this.userForm.phone);

      if (!result) {
        this.phoneSpan = '请输入正确的手机号';
        this.checkPhoneNum = 0;
        this.registerCommit = true;
        return;
      }
      await checkInfo({ phone: this.userForm.phone }).then(res => {
        if (res.code !== 1) {
          if (this.checkPhoneNum !== 0) {
            this.checkPhoneNum = 0;
          }
          this.phoneSpan = '手机号已注册存在';
          this.registerCommit = true;
        } else {
          if (this.checkPhoneNum !== 1) {
            this.checkPhoneNum = 1;
          }
          this.phoneSpan = '';
          if (this.userNameCheckNum === 1 && this.checkPhoneNum === 1) {
            this.registerCommit = false;
          }
        }
      });
    },
    handleClose () {
      this.userForm.userName = '';
      this.userForm.passWord = '';
      this.userForm.phone = '';
      this.$refs.registerRef.resetFields();
      this.userNameSpan = '';
      this.phoneSpan = '';
      this.dialogVisible = false;
    },
    register () {
      this.$refs.registerRef.validate(async validate => {
        if (!validate) {
          return false;
        }
        await register(this.userForm).then(res => {
          if (res.code !== 1) {
            return this.$message.error(res.msg);
          }
          this.$message.success(res.msg);
        });
        this.userForm.userName = '';
        this.userForm.passWord = '';
        this.userForm.phone = '';
        this.$refs.registerRef.resetFields();
        this.userNameSpan = '';
        this.phoneSpan = '';
        this.dialogVisible = false;
      });
    }
  }
};
</script>
<style lang="less" scoped>
.main {
  width: 100%;
  height: 100%;
}
.images_box {
  width: 1318px;
  height: 461px;
  background: url(../../assets/images/login/247fc2a069d096bf4e66fcd02e91317f.png)
    no-repeat 50%;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.form_box {
  width: 500px;
  height: 400px;
  margin: 0 auto;
  text-align: center;
  font-size: 20px;
  color: #5e6166;
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 0 50px #ecf4f8;
  position: relative;
  // left: 470px;
  top: 50px;
  .login_form {
    margin-right: 15px;
    margin-top: 30px;
  }
}
.login_form /deep/ .el-form-item {
  margin-top: 30px;
  .el-form-item__label {
    font-size: 18px;
  }
  .el-form-item__content .el-input {
    .el-input__inner {
      //width: 300px;
      border: 1px solid green;
    }
    .el-input__prefix .el-input__icon {
      margin-left: 5px;
    }
  }
}
.login_form /deep/ .el-button {
  margin-top: 10px;
  width: 100px;
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #ffffff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eeeeee;
  }
}
.el-icon-lock {
  margin-top: -50px;
}

h1 {
  padding-top: 100px;
  width: 100%;
}
.register_form /deep/ .el-form-item {
  .el-form-item__content {
    .span {
      color: #f56c6c;
      font-size: 12px;
      line-height: 1;
      padding-top: 4px;
      position: absolute;
      top: 100%;
      left: 0;
    }
  }
}
</style>
